package com.hsmpay.utils;

import it.sauronsoftware.base64.Base64;
import org.apache.log4j.Logger;
import com.hsmpay.utils.system.ConConfig;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.Security;

public class ZytlHttpUrlToPost {
	public static String urlStr = ConConfig.getAttribute("ZYTL_DAISHOU_URL_PATH");
	
	private static Logger log = Logger.getLogger(ZytlHttpUrlToPost.class);
	static{//安全性
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	}

	public static String jami(String json,String des3Key,String mcode)throws Exception{
		String terminalTypeId = new String(Base64.encode(mcode.getBytes("UTF-8")),"UTF-8");
		RSAObjectCZYTL rsaEncrypt= RSAObjectCZYTL.getInstance();
		byte[] desKeyB = rsaEncrypt.encryptNew(rsaEncrypt.getPublicKey(), des3Key.getBytes("Utf-8"));
		String desKey = new String(Base64.encode(desKeyB),"UTF-8");
		String data = Des3.encode(json,des3Key);
		String str = terminalTypeId+"|"+desKey+"|"+data;
		return str;
	}
	
	/**
	 * 为了扩展 数据库保存 URL  公钥 这里使用传值
	 * @param json
	 * @param urlStr
	 * @return
	 * @throws Exception
	 */
	public static String httpClientUtils(String json,String urlStr,String mcode) throws Exception{
		log.warn("请求URL:" + urlStr);
		log.warn("请求参数:" + json);
		String des3Key = Des3.generate32Key();
		log.warn("des3Key:" + des3Key);
		String xmlString=jami(json,des3Key,mcode);
		String Method="POST";

		byte[] xmlData = null;
		try {
			xmlData = xmlString.getBytes("UTF-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		BufferedReader rd =null;
		String msg="";
		
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
			urlCon.setDoOutput(true);
			urlCon.setDoInput(true);
			urlCon.setUseCaches(false);
			urlCon.setRequestMethod(Method);
			urlCon.setConnectTimeout(120000);
			urlCon.setReadTimeout(120000);
			urlCon.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
			urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
			urlCon.setRequestMethod("POST");
			DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
			printout.write(xmlData);
			printout.flush();
			printout.close();
			log.warn("数据发送完成,等待响应...");
			rd = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"utf-8"));  
			String inputLine;
			while ((inputLine = rd.readLine()) != null)
				msg+=inputLine;
			log.warn("解密前数据:"+msg);
			//解密
			String[] strArray = msg.split("\\|");
			if(strArray.length == 3){
				if("1".equals(strArray[0])){//成功信息
					log.warn("密文:"+strArray[1]+"#");
					log.warn("deskey:"+des3Key+"#");
					String data = Des3.decode(strArray[1],des3Key);
					msg = data;
					log.warn("响应数据为:"+msg);
				}else{
					String code = strArray[1];
					log.warn("错误代码:"+code);
					String eMsg = new String(Base64.decode(strArray[2].getBytes("UTF-8")),"UTF-8");
					log.warn("错误描述:"+eMsg);
                    msg = "{\"reqCode\":\""+code+"\",\"reqMsg\":\""+eMsg+"\"}";
                }
			}
			rd.close();				
		}catch (Exception e) {
			throw e;
		}
		return msg;
	}
}